<?php

namespace App\Http\Requests\User\Personal;

use App\Models\Personal;
use App\Models\System\Setting;
use App\Exceptions\GeneralException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Foundation\Http\FormRequest;

class ModifyProfile extends FormRequest
{
	protected $rules = [];

	/**
	 * Date: 27/04/2018
	 * @author George
	 * @return bool
	 */
    public function authorize()
    {
        return true;
    }

	/**
	 * 获取规则
	 *
	 * Date: 27/04/2018
	 * @author George
	 * @return array
	 * @throws GeneralException
	 */
    public function rules()
    {
		if (! $this->get('company_id')) {
			throw new GeneralException('请选择公司', 500);
		}

		$personal = Personal::query()->where('identification')->count();

		if ($personal > 0) {
			throw new GeneralException('记录已存在', 500);
		}

		return  Cache::remember("companies:{$this->company_id}:rules", 360, function () {
			$result = Setting::query()->select(['company_id', 'register'])->where('company_id', $this->company_id)->first();
			return optional($result)->register;
		});
    }
}
